import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Practice3 {
    /*
    https://www.nowcoder.com/practice/e88b41dc6e764b2893bc4221777ffe64?tpId=308&tqId=40477&ru=/exam/oj
    bfs走迷宫问题, 思路要清晰, 注意一下下标的映射关系
     */
    public static int dx[] = {0,0,-1,1};
    public static int dy[] = {1,-1,0,0};
    public static char[][] arr;
    public static int n;
    public static int m;
    public static int x2;
    public static int y2;
    public static boolean vis[][];
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        int x1 = in.nextInt(),y1 = in.nextInt();
        x2 = in.nextInt();
        y2 = in.nextInt();
        //存储迷宫路径, 以及判断不要走重复路线
        arr = new char[n][m];
        vis = new boolean[n][m];
        for(int i = 0;i < n;i++){
            arr[i] = in.next().toCharArray();
        }
        //创建函数, 返回答案, 传入入口
        System.out.println(bfs(x1 - 1,y1 - 1));
    }

    private static int bfs(int a,int b){
        //创建队列, 将初始位置传入
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{a,b});
        //标记已走过, 并设置计数器来判断走了多少步
        vis[a][b] = true;
        int count = 0;
        while(!q.isEmpty()){
            //每次判断q中的元素, 并步数＋1
            int size = q.size();
            count++;
            while(size-- > 0){
                int[] t = q.poll();
                for(int i = 0;i < 4;i++){
                    //四个方向判断一下
                    int d = t[1];
                    int c = t[0];
                    int x = c + dx[i];
                    int y = d + dy[i];
                    if(x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && arr[x][y] == '.'){
                        q.add(new int[]{x,y});
                        vis[x][y] = true;
                        //如果满足的话, 直接返回步数, 条件里的-1是保持下标的映射
                        if(x == x2 - 1 && y == y2 - 1){
                            return count;
                        }
                    }
                }
            }
        }
        return -1;

    }
}